/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista.planificacion;

import controlador.planificacion.GestorEditarPlanificacion;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import modelo.TareaPlanificacion;
import vista.gui.dnd.PanelDropTarget;
import vista.planificacion.arbolTareas.ArbolIconoNodo;
import vista.planificacion.arbolTareas.ArbolIconoRenderer;

/**
 *
 * @author Administrador
 */
public class PlanificacionSubTareasArbolHerencias extends javax.swing.JInternalFrame {

    private ArbolTareasGestor arbolTareasGestor;
    private GestorEditarPlanificacion _gestorPadre;
    private TareaPlanificacion tarea;
    
    /**
     * Creates new form PlanificacionSubTareasArbolHerencias
     */
    public PlanificacionSubTareasArbolHerencias(GestorEditarPlanificacion _gestorPadre,TareaPlanificacion tarea) {
        this._gestorPadre = _gestorPadre;
        this.tarea = tarea;
        initComponents();
        arbolTareasGestor = new ArbolTareasGestor(arbolTareas);
        inicializarArbolDeTareas();
    }

    /**
     * Inizializar el Arbol de tareas
     */
    private void inicializarArbolDeTareas()
    {
        DefaultTreeModel modelo=_gestorPadre.cargarModeloArbolTareasSoloTareas();
        arbolTareas.setModel(modelo);
        arbolTareas.setCellRenderer(new ArbolIconoRenderer());
        arbolTareas.repaint();  
    }    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        arbolTareas = new javax.swing.JTree();
        btnCancelar = new javax.swing.JButton();
        btnCambiarPadre = new javax.swing.JButton();

        setClosable(true);
        setResizable(true);
        setTitle("Seleccione la nueva tarea Contenedora");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Seleccione la nueva tarea que contendrá a la tarea actual");

        jScrollPane1.setViewportView(arbolTareas);

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/block.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnCambiarPadre.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/tag_blue.png"))); // NOI18N
        btnCambiarPadre.setText("Cambiar Tarea de Lugar");
        btnCambiarPadre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCambiarPadreActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnCambiarPadre)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnCambiarPadre))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnCambiarPadreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCambiarPadreActionPerformed
        
        TreePath tp = arbolTareas.getSelectionPath();
        final ArbolIconoNodo nodo  = ArbolTareasGestor.getNodoDeTreeNodePath(tp);
        if(nodo!=null)
        {
            // Es la misma tarea padre?
            TareaPlanificacion padre = this.tarea.buscarCaminoHastaTareaConCotizacion(_gestorPadre.getPlanificacion(),true,true).get(0);
            if(padre!=null)
            {
                if(padre.hashCode()==nodo.getId())
                {
                    StringBuilder msg = new StringBuilder("<HTML>");
                    msg.append("La tarea de <b>");
                    msg.append(this.tarea.getNombre());
                    msg.append("</b> ya está siendo contenida por la tarea <b>");
                    msg.append(padre.getNombre());
                    msg.append("</b><br/>");
                    msg.append("<i>Por favor seleccione otra tarea</i>");
                    JOptionPane.showInternalMessageDialog(this,msg.toString(),"Atención!",JOptionPane.INFORMATION_MESSAGE);
                    return;
                }                  
            }
            
            
            // Es al misma tarea ?
            if(nodo.getId()==tarea.hashCode())
            {
                StringBuilder msg = new StringBuilder("<HTML>");
                msg.append("La tarea destino: <b>");
                msg.append(nodo.getTitulo());
                msg.append("</b><br>es la misma que la tarea origen: <b>");
                msg.append(this.tarea.getNombre());
                msg.append("</b><br/>");
                msg.append("<i>Por favor seleccione otra tarea</i>");
                JOptionPane.showInternalMessageDialog(this,msg.toString(),"Atención!",JOptionPane.INFORMATION_MESSAGE);
                return;
            }            
            // Confirmar ?
            StringBuilder msg = new StringBuilder("<HTML>");
                msg.append("Está seguro que desea que la tarea:");
                msg.append("<br/>");
                msg.append("<b>");
                msg.append(nodo.getId());
                msg.append("-");
                msg.append(nodo.getTitulo());
                msg.append("</b> ");
                msg.append("<br/>");
                msg.append("sea la nueva tarea contenedora de:");
                msg.append("<br/>");
                msg.append("<b>");
                msg.append("bla bla");
                msg.append("</b>");
                msg.append("?");
                
                Object[] options = {"Si, continuar","No, cancelar"};
                int n = JOptionPane.showInternalOptionDialog(this,msg.toString(),"Atencion!",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,null,options,options[1]);            
                System.out.println("SELECCIONO: "+n);

                switch(n)
                {
                    case 0:
                       // Si
                        if(megaAlgoritmoQueNoEntiendoDeCambioDeHoras(this.tarea,padre))
                        {
                            // Se cambio correctamente
                        }
                        else
                        {
                            // Error al cambiar
                        }
                       break;
                    default:
                        // No, nadaremos
                }
        }
        
    }//GEN-LAST:event_btnCambiarPadreActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree arbolTareas;
    private javax.swing.JButton btnCambiarPadre;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private boolean megaAlgoritmoQueNoEntiendoDeCambioDeHoras(TareaPlanificacion hija, TareaPlanificacion padre) {
        
        System.out.println("[DEBUG] Moviendo la Tarea "+hija+" dentro de la tarea"+padre);
        
        
        return false;
    }
}
